<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-11-22 17:14
 *
 * Version: 2.0
 */

namespace App\Modules\Coupon\Controllers;

use App\Lib\Facades\FLAuth;
use App\Modules\Coupon\Services\CouponService;
use App\Modules\Coupon\Validators\CouponBossControllerValidator;
use Flower\Core\Http\BaseController;
use Flower\Facades\Flower;
use Illuminate\Http\Request;

class CouponBossController extends BaseController
{
	protected $couponService;

	public function __construct(CouponService $couponService)
	{
		$this->couponService = $couponService;
	}

	public function getRules()
	{
		return CouponBossControllerValidator::getRules();
	}


	/**
	 * 新建优惠券
	 * @param Request $request
	 * @return mixed
	 */
	public function create(Request $request){
		$params = $request->all();
		$params['user_id'] = FLAuth::id();
		$info = $this->couponService->create($params);
		return $this->display($info->id);
	}


	/**
	 * 编辑优惠券
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\AllValidatorException
	 * @throws \App\Exceptions\NotFoundException
	 */
	public function edit(Request $request){
		$params = $request->all();
		$info = $this->couponService->edit($params);
		return $this->display($info->id);
	}


	/**
	 *  删除优惠券
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\NotFoundException
	 */
	public function del(Request $request){
		$this->couponService->del($request->input('id'));
		return $this->success();
	}

	/**
	 * 发布优惠券
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\NotFoundException
	 */
	public function post(Request $request){
		$this->couponService->post($request->input('id'));
		return $this->success();
	}


	/**
	 * 优惠券回收
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\NotFoundException
	 */
	public function back(Request $request){
		$storeId = $request->input('store_id');
		$userId = FLAuth::id();
		$result = $this->couponService->backNoReceiveCoupon($userId,$storeId);
		return $this->handleApiResult($result);
	}


	/**
	 * 优惠券使用情况
	 * @param Request $request
	 * @return mixed
	 */
	public function couponSendAndUsedList(Request $request){
		$couponId = $request->input('coupon_id');
		$isUsed = $request->input('is_used');
		$result = $this->couponService->couponSendAndUsedList($couponId,$isUsed);
		return $this->handleApiResult($result);
	}




}